package missile;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import tower.maintower;
import creep.Creep;

public class Missile{

	private double speed;
	private int dx, dy, x, y;
	private Image img;
	private Creep creep;
	private maintower tower;
	private boolean visible;
	private int width;
	private int height;
	private int power;
	
	public Missile (double speed, Image img, Creep creep, maintower tower, int power, int cellSize){
		this.speed= speed;
		this.img= img;
		this.creep= creep;
		this.tower= tower;
		this.x= tower.getPositionX()+ cellSize/2 -3;
		this.y= tower.getPositionY()+ cellSize/2 -2;
		this.width=10;
		this.height=10;
		this.power=power;
		this.visible= this.creep == null ? false : true;

	}
	
	public double getSpeed(){
		return this.speed;
	}
	
	public int getPower(){
		return this.power;
	}
	
	public int getDX(){
		return this.dx;
	}
	
	public int getDY(){
		return this.dy;
	}
	
	public int getX(){
		return this.x;
	}
	
	public int getY(){
		return this.y;
	}
	
	public Image getImage(){
		return this.img;
	}
	
	public Creep getCreep(){
		return this.creep;
	}
	
	public maintower getTower(){
		return this.tower;
	}
	
	public boolean isVisible(){
		return this.visible;
	}
	
	public int getWidth() {
		return this.width;
	}
	public int getHeight() {
		return this.height;
	}
	
	public void setDX(int dx){
		this.dx= dx;
	}
	
	public void setPower(int power){
		this.power=power;
	}
	
	public void setDY(int dy){
		this.dy= dy;
	}
	
	public void setX(int x){
		this.x= x;
	}
	
	public void setY(int y){
		this.y= y;
	}
	
	public void setSpeed(int speed){
		this.speed= speed;
	}
	
	public void setImage(Image img){
		this.img= img;
	}
	
	public void setCreep(Creep creep){
		this.creep= creep;
	}
	
	public void setTower(maintower tower){
		this.tower=tower;
	}
	
	public void setVisible(boolean b){
		this.visible= b;
	}
	
	public void setWidth(int w) {
		this.width = w;
	}
	public void setHeight(int h) {
		this.height = h;
	}
	
	//sender missilet i retning creepet
	public void setDirection(){
		int x= (this.creep.getX() - this.getX())/8;
		int y= (this.creep.getY() - this.getY())/8;
		this.dx= x;
		this.dy= y;
	}
	
	public void move() {
		setDirection();
		this.x += this.dx*this.speed;
		this.y += this.dy*this.speed;
		this.speed+=1.0;
		this.hasHit();
	}
	
	public Rectangle getBounds() {
        return new Rectangle(getX(), getY(), getWidth(), getHeight());
    }
	
	public void hasHit(){
		Rectangle rec= new Rectangle(getX(), getY(), getWidth(), getHeight());
		if(this.creep.getBounds().intersects(rec)) hit();
	}
	
	public void hit(){
		this.getCreep().hit(this.getPower());
		this.setVisible(false);
	    }
	
}